
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.ClienteDAO;

public class GerenciaCliente extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
           int op = Integer.parseInt(request.getParameter("op1"));
           ClienteDAO DAO = new ClienteDAO();
           if(op==1){
                String nome = request.getParameter("nome");
                String cpf = request.getParameter("cpf");
                String endereço = request.getParameter("endereço");
                String sexo = request.getParameter("sexo");
                String idade = request.getParameter("idade");
           
                Cliente Novo = new Cliente();
           
                Novo.setNome(nome);
                Novo.setCpf(cpf);
                Novo.setEndereço(endereço);
                Novo.setSexo(sexo);
                Novo.setIdade(idade);
                String resposta = DAO.GravarDados(Novo);
                out.println(resposta);
           }else{
            List<Cliente> clientes = DAO.ConsultarDados();
               for(Cliente retorna : clientes){
                   out.println("Nome: "+ retorna.getNome()+"<br>"+
                           "cpf: "+ retorna.getCpf()+"<br>"+
                           "endereço: "+retorna.getEndereço()+"<br>"+
                           "sexo: "+retorna.getSexo()+"<br>"+
                           "idade: "+retorna.getIdade()+"<br>"+
                           "<input type="+"button"+" value="+"excluir"+" onclick="+"Deletar("+3+","+retorna.getCpf()+")"+"/>"+
                           "<hr>"
                   );
               }
            if(op==3){
                String PrimaryKey = request.getParameter("PrimaryKey");
                ClienteDAO N = new ClienteDAO();
                N.Deletar(PrimaryKey);
                out.println("deletado com sucesso!");
            }
               
           }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(GerenciaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(GerenciaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
